var kmMaintance = {
    productscrollLoading :true,
	competscrollLoading :true,
	/* 初始化左边树结构
	*/
	initLeftTree: function () {
		//<!--
		//调用后台接口，取得树形结构的列表
		//转圈
		var url = "/PCIndustryChart/pc/ztreeQuery";
		//转圈
		common.showLoading();
		common.interfaceAjaxFunction(url, "", this.showLeftPartTreeSuccess1,true,"",true);

		//-->
	 },


	/**
	* 查询所有产品,竞品
	*/
	queryAllProduct: function (type, url) {

		//调用后台接口，取得数据，然后拼接
		var param = {};
		if (type == "compat") {
			kmMaintance.competscrollLoading=true;
			param = {
				type: "1",
				pageNum: common.compatCurrentPage,
				pageSize: 20,
			};
		}
		if (type == "product") {
			kmMaintance.productscrollLoading =true;
			param = {
				type: "1",
				pageNum: common.productCurrentPage,
				pageSize: 20,
			};
		}

		//转圈
		common.showLoading();
		var url = url;
		common.interfaceAjaxFunction(url, param, this.queryProductSuccess, false, type);
	},
	/**
	 * 查询所有产品接口返回值处理
	 */
	queryProductSuccess: function (json, param) {
		var container = "";
		if (param == "product") {
		container = "queryAllProductkm";
		} else {
			container = "queryCompatiblekm";
		}
		//成功时候
		if (json.status == "0") {
		if (json.data.length > 0) {
			//拼接显示数据
		for (var i = 0; i < json.data.length; i++) {
			var thisRowData = json.data[i];
			if (param == "product") {
		var recentKnowledgesStr = "<li class='allProductList'title='" + thisRowData.product_name + "'><a  data-pk='" + thisRowData.pk_product + "'>" + thisRowData.product_name + "</a></li>"
		} else {
			var recentKnowledgesStr = "<li class='allProductList'title='" + thisRowData.compatible_product_name + "'><a  data-pk='" + thisRowData.pk_compatible + "'>" + thisRowData.compatible_product_name + "</a></li>"
		}
		//加入
		$("#" + container).append(recentKnowledgesStr);
		//单击事件的绑定
		$(".allProductList").unbind().on("click", function () {
		//记录当前的type
		window.sessionStorage.searchKmType = "";
		window.sessionStorage.searchOrderKey = "publish_time";
		window.sessionStorage.searchKmKey = $(this).find("a").html();
		$("#searchInput").val($(this).find("a").html());
		window.location.href = "#/moreKmInfoList/" + $(this).find("a").html();
				});
			}
		} else {
			if(param == "product"){
		if(common.productCurrentPage == 1){
			common.show_error("当前无最新数据",container);
		}else{
			kmMaintance.productscrollLoading=false;
			layer.msg('已加载全部产品');
			}
		}else{
			if(common.compatCurrentPage == 1){
				common.show_error("当前无最新数据",container);
		}else{
			kmMaintance.competscrollLoading=false;
			layer.msg('已加载全部竞品');
			}
		}
		//加载设置页面
			
		}
		    	} else {
		//给出信息提示
		common.show_error(json.message);
		    	}
		    	//转圈提示去除
		common.hideLoading();
		common.loadingData = false;
	},

	/**
	 * 查询知识类别的信息列表
	 */
	initCategoryList:function(){
		//调用后台接口，取得类别的列表
		var param = {
			pk_user:window.localStorage.userid	
		};
		//转圈
		common.showLoading();
		var url = "/PCKnowledgeCategoryEntity/queryAllKnowledgeCateTree";
		common.interfaceAjaxFunction(url,param,this.getAllKnowledgeCategorySuccess,true,"",true);
		
	},
	/**
	 * 调用取得树的结构,然后进行数据的渲染
	 */
	getAllKnowledgeCategorySuccess:function(json){
		//成功时候
    	if(json.status == "0"){
    		if(json.data.length > 0){
    			//先清空
    			$("#mainCategoryList").html("<li><a href='javascript:void(0);' class='shellKmTypeTitle' data-pk=''>全部</a></li>");
	    		//显示数据
    			for(var i=0;i<json.data.length;i++){
    				if(i<=4){
    				var categoryRow = json.data[i];
    				var categoryRowChild = categoryRow.childList;
    				//根据有无孩子进行处理
    				var categoryRowList = "";
    				if(categoryRowChild.length > 0){
    					categoryRowList +="<li class='dropdown'>"
    						+"<a href='javascript:void(0);' class='shellKmTypeTitle' data-pk='"+categoryRow.pk_knowledge_category+"'>" + categoryRow.knowledge_category_name  + "</a>"
    						+"<a href='#' class='dropdown-toggle' data-toggle='dropdown' aria-expanded='true'>"
    						+"<b class='caret'></b>"
    						+"</a>"
    						+"<ul class='dropdown-menu shell-select-bj'>";
    					//循环孩子节点
    					for(var j=0;j<categoryRowChild.length;j++){
    						var categoryRowChildRow = categoryRowChild[j];
    						categoryRowList +="<li><a href='javascript:void(0);' class='shellKmTypeTitle' data-pk='"+categoryRowChildRow.pk_knowledge_category+"'>"+categoryRowChildRow.knowledge_category_name+"</a></li>";
    					}
    					//继续拼接数据
    					categoryRowList +="</ul>"
    						+"</li>";
    				}else{
    					categoryRowList = "<li><a href='javascript:void(0);' class='shellKmTypeTitle' data-pk='"+categoryRow.pk_knowledge_category+"'>"+categoryRow.knowledge_category_name+"</a></li>";
    				}
    				//显示数据
    				$("#mainCategoryList").append(categoryRowList);
    				////console.log("显示数据显示数据显示数据显示数据显示数据显示数据显示数据");
    				}
    			}
    			//事件的绑定
    			$("#mainCategoryList .shellKmTypeTitle").unbind().bind("click",function(){
    				//当前类别id
    				var thisTypePk = $(this).attr("data-pk");
    				if(thisTypePk == ""){
    					$("#searchInput").val();
    					window.sessionStorage.searchKmType = "";
	    				window.sessionStorage.searchOrderKey = "publish_time";
	    				window.location.href = "#/moreKmInfoList/searchAll";
    				}else{
    					//查询条件
	    				window.sessionStorage.searchKmType = thisTypePk;
	    				window.sessionStorage.searchOrderKey = "publish_time";
	    				window.location.href = "#/moreKmInfoList/" + thisTypePk;
    				}
    			});
    		}else{
    			//加载设置页面
    			layer.msg("当前无类别数据，请联系管理员维护！");
    		}
    	}else{
    		//给出信息提示
    		common.show_error(json.message);
    	}
    	//转圈提示去除
    	common.hideLoading();
	},



	/**
	 * 调用取得树的结构,然后进行数据的渲染
	 */
	showLeftPartTreeSuccess1: function (json) {
		//成功时候
		if (json.status == "0") {
			if (json.data.length > 0) {
				//加载设置后的主页面
				kmMaintance.showLeftPartTree(json.data);
			} else {
				//加载设置页面
				layer.msg("当前无行业树结构数据，请联系管理员维护");
			}
    	} else {
    		//给出信息提示
    		layer.msg(json.message);
    	}
    	//转圈提示去除
		common.hideLoading();
	},
	/**
	 * 显示showLeftPartTree函数，渲染显示数据
	 */
	showLeftPartTree: function (jsonData) {
		var setting = {
			data: {
				key: {
					title: "title"
				},
				simpleData: {
					enable: true
				}
			}, callback: {
				onClick: kmMaintance.onTreeNodeClick
			}
		};
		//公共用的树结构
		var zNodes = [];
		//循环得到的data数据，然后进行push到zNodes中去
		for (var i = 0; i < jsonData.length; i++) {
			var jsonDataRow = jsonData[i];
			var jsonStruct = {
				id: jsonDataRow.pk_industry_chart,
				pId: jsonDataRow.fw_parent,
				name: jsonDataRow.cate_name,
				title: jsonDataRow.cate_name
			};
			//压入到树结构中去
			zNodes.push(jsonStruct);
		}
		$.fn.zTree.init($("#leftTree"), setting, zNodes);
	},
	/**
	 * 单击的时候调用查询事件
	 */
	onTreeNodeClick: function (event, treeId, treeNode, clickFlag) {
		//layer.msg(treeNode.level + ",clickFlag ==" + clickFlag);
		//设备和工序上面加上单击事件
		if (clickFlag == "1") {// && (treeNode.level == "2" || treeNode.level == "3")
			//事件绑定
			//记录当前的type
			window.sessionStorage.searchKmType = "";
			window.sessionStorage.searchKmKey = treeNode.name;
			window.sessionStorage.searchOrderKey = "publish_time";
			$("#searchInput").val(treeNode.name);
			window.location.href = "#/moreKmInfoList/" + treeNode.name;
		}
	},
	
    bindRoot: function () {
		var url = "/PCpublish/queryKmpublishPoJoLikePage";
		var userid = window.localStorage.userid;
		var order_Rule = window.sessionStorage.getItem("order_Utype");
		var json = window.sessionStorage.loginUserInfo;
	    var obj = JSON.parse(json);
	    var array = obj.data.pk_category;
	     
		map ={
				"pk_category":array,
				"pk_user": userid,
				"pageSize": common.pageSize,
				"pageNum": common.currentPage,
				"publish_key": "",
				"km_type": "",
				"paramOrder": window.sessionStorage.order_Uid ?window.sessionStorage.order_Uid :"publish_time",
				"orderRule": order_Rule ? order_Rule : 'desc'
			};
		common.showLoading();
		common.interfaceAjaxFunction(url, map, kmMaintance.showLeftPartTreeSuccess, true);
	},
	 bindRoottitle: function () {
			var url = "/PCpublish/queryKmpublishPoJoLikePage";
			var userid = window.localStorage.userid;
			var order_Rule = window.sessionStorage.getItem("order_Utype");
			var json = window.sessionStorage.loginUserInfo;
		    var obj = JSON.parse(json);
		    var array = obj.data.pk_category;
		     
			map ={  "pk_category":array,
					"pk_user": userid,
					"pageSize": common.pageSize,
					"pageNum": common.currentPage,
					"publish_key": "",
					"km_type": "",
					"paramOrder": window.sessionStorage.order_Uid,
					"orderRule": order_Rule ? order_Rule : 'desc'
				};
			common.showLoading();
			common.interfaceAjaxFunction(url, map, kmMaintance.showLeftPartTreeSuccess, true);
		},
		/**
		 * 调用取得树的结构,然后进行数据的渲染
		 */
		showLeftPartTreeSuccess: function (json) {
			$("#frm").empty();
			var jsonData = json.data;
			var cssSta = "";
			//成功时候
			if (json.status == "0") {
				if (json.data.length > 0) {
					var processListStr = "";
					for (var i = 0; i < jsonData.length; i++) {
						if (jsonData[i].sealmark == "Y") {
							cssSta = "btn-unlock";
						} else {
							cssSta = "btn-disable";
						}
						var publishName = jsonData[i].pk_publish_user == undefined ? "" : jsonData[i].pk_publish_user;
						processListStr += '<tr >' +
							'<td ><a class="blue-color waitcallback-content publish_title" pk_publish="' + jsonData[i].pk_km_publish + '">' + jsonData[i].publish_title + '</a></td>' +
							'<td>' + jsonData[i].publish_key + '</td>' +
							'<td >' + jsonData[i].knowledge_category_name + '</td>' +
							'<td >' + jsonData[i].nuu + '</td>' +
							'<td>' + jsonData[i].ter + '</td>' +
							'<td>' + publishName + '</td>' +
							'<td >' + jsonData[i].publish_time + '</td>' +

							'<td><div class="operation"><a class="btn-editor btonUpdate" setdatadd="' + jsonData[i].pk_km_publish + '"></a>' +
							'<a class="btn-delete bton" setdatadd1="' + jsonData[i].pk_km_publish + '" Aname="' + jsonData[i].publish_title + '"></a>' +
							'<a class="' + cssSta + ' onfale" setdata="' + cssSta + '" zsid="' + jsonData[i].pk_km_publish + '"></a></div></td>' +
							'</tr>'
						$("#frm").html(processListStr);
					}
					var lockerStatus = window.sessionStorage.getItem("lockerStatus");
					if (lockerStatus == '2') {
						layer.msg("停用成功!");
					} if (lockerStatus == '3') {
						layer.msg("启用成功!");
					}
					//启用/禁用
					$(".onfale").unbind().on("click", function () {
						var fale = $(this).attr("setdata");
						var zsid = $(this).attr("zsid");
						url = "/PCpublish/updateSealmark";
						map = { "fale": fale, "zsid": zsid };
						//转圈
						common.showLoading();
						common.interfaceAjaxFunction(url, map, kmMaintance.initFal, true);
					});
					//跳转到知识详情
					// 弹出新的窗口查看详情
					$(".publish_title").unbind().on("click", function () {
						var pk_publish = $(this).attr("pk_publish");
						window.open("html/kmMaintanceReading.html?pk_publish=" + pk_publish);
					});
					//删除
					$(".bton").unbind().on("click", function () {
						var all = $(this).attr("setdatadd1");
						var Aname = $(this).attr("Aname");
						url = "/PCpublish/delPublishByid";
						map = { "pk_km_publish": all };
						layer.confirm("您确认要删除知识：" + Aname + "吗？", { area: ['400px', '200px'], title: '删除' }, function () {
							//转圈
							common.showLoading();
							common.interfaceAjaxFunction(url, map, kmMaintance.deleteTreeSuccess);
						});

					});
					//修改 绑定事件 
					$(".btonUpdate").unbind().on("click", function () {
						var all = $(this).attr("setdatadd");
						window.sessionStorage.AllId = all;
						common.loadPageDiv("html/kmMaintanceUpdate.html", "kmMaintanceAddRightPart", true);
					});

	    			/*//单击查询时候进行事件监听
	    			$("#searchButton").unbind().on("click",function(){
	    				window.sessionStorage.searchKmKey = $("#searchInput").val();
	    				window.sessionStorage.searchKmType = "";
	    				window.sessionStorage.searchOrderKey = "publish_time";
	    				window.location.href = "#/moreKmInfoList/" + ($("#searchInput").val() == "" ? "search" : $("#searchInput").val());
	    			});*/
				} else {
					$("#frm").html("<tr><td  colspan='8'>暂无知识数据</td></tr>");
				}
				common.getPageDivide(json.count, "pageDivideContainer", kmMaintance.bindRoot);
			} else {
				//给出信息提示
				common.show_error(json.message);
			}
			common.hideLoading();
		},
		initFal: function (json) {
			window.sessionStorage.setItem("lockerStatus", json.num);
			kmMaintance.bindRoot();
			common.hideLoading();
		},
		//删除的回调函数
		deleteTreeSuccess: function (json) {
			if (json.status == "0") {
				layer.msg(json.message);
				kmMaintance.bindRoot()
			} else if (json.status == "1") {
				layer.msg(json.message);
			}
			common.hideLoading();
		},
		//绑定事件
		bindEvent: function () {
			$("#kmMaintanceAddBtn").unbind().on("click", function () {
				common.loadPageDiv("html/kmMaintanceAdd.html", "kmMaintanceAddRightPart", true);
			});
			
			$("#layout-t span:first").addClass("current");
			$("#layout-t .tab-bd-con:gt(0)").hide();
			
			$(".tab-hd-con").click(function(){//mouseover 改为 click 将变成点击后才显示，mouseover是滑过就显示
				$(this).addClass("current").siblings("span").removeClass("current");
				$("#layout-t .tab-bd-con:eq("+$(this).index()+")").show().siblings(".tab-bd-con").hide().addClass("current");
				//查询所有产品 
				if($(this).index() == "1" && $("#queryAllProductkm").html() == ""){
					kmMaintance.queryAllProduct("product","/pcBdproduct/queryProductPage");
				}else if($(this).index() == "2" && $("#queryCompatiblekm").html() == ""){
					//查询竞品产品
					kmMaintance.queryAllProduct("compat","/pcBdCompatible/queryCompatible");
				}
			});
			$(".title").unbind().on("click", function () {
				window.sessionStorage.setItem("lockerStatus","0");
				var down = '<img src="../admin/images/down.png"/>';
				var uap = '<img src="../admin/images/uap.png"/>';

				//排序规则 首先清空所有的图片，然后根据
				if ($(this).attr("data-type") == "ASC") {
					$(".waitcallback-uap").html("");
					window.sessionStorage.order_Utype = "DESC";
					$(this).attr("data-type", "DESC");
					$(this).children(".waitcallback-uap").html(down);
				} else {
					$(".waitcallback-uap").html("");
					window.sessionStorage.order_Utype = "ASC";
					$(this).attr("data-type", "ASC");
					$(this).children(".waitcallback-uap").html(uap);
				}
				//保存数据
				window.sessionStorage.order_Uid = $(this).attr("id");
				kmMaintance.bindRoottitle();
			});
			//点击搜多矿查询
			$("#searchButton").unbind().on("click", function () {
				window.sessionStorage.searchKmKey = $("#searchInput").val();
				window.sessionStorage.searchKmType = "";
				window.sessionStorage.searchOrderKey = "publish_time";
				var json = window.sessionStorage.loginUserInfo;
			    var obj = JSON.parse(json);
			    var array = obj.data.pk_category;
			     
				var param = {
					"pk_category":array,
					"pk_user": window.localStorage.userid,
					"pageSize": common.pageSize,
					"pageNum": common.currentPage,
					"publish_key": window.sessionStorage.searchKmKey,
					"km_type": window.sessionStorage.searchKmType,
					"paramOrder": window.sessionStorage.searchOrderKey,
					"orderRule": "ASC"
				};
				var url = "/PCpublish/queryKmpublishPoJoLikePage";
				//转圈
				common.showLoading();
				common.interfaceAjaxFunction(url, param, kmMaintance.showLeftPartTreeSuccess);
			});

			$("#queryCompatiblekm").scroll(function () {
				if (kmMaintance.competscrollLoading) {
					var viewH = $(this).height();//可见高度  
					var contentH = $(this).get(0).scrollHeight;//内容高度  
					var scrollTop = $(this).scrollTop();//滚动高度  
					var height = contentH - viewH - scrollTop;
					if (height <= 100 && !common.loadingData) {
						common.loadingData = true;
						common.compatCurrentPage += 1;
						kmMaintance.queryAllProduct("compat", "/pcBdCompatible/queryCompatible");
					}
				}else{
					return;
				}
			});
			$("#queryAllProductkm").scroll(function () {
				if (kmMaintance.productscrollLoading) {
					var viewH = $(this).height();//可见高度  
					var contentH = $(this).get(0).scrollHeight;//内容高度  
					var scrollTop = $(this).scrollTop();//滚动高度  
					var height = contentH - viewH - scrollTop;
					if (height <= 100 && !common.loadingData) {
						common.loadingData = true;
						common.productCurrentPage += 1;
						kmMaintance.queryAllProduct("product", "/pcBdproduct/queryProductPage");
					}
				}else{
					return;
				}
			});

		},
		/**
		 * 初始化窗口高度
		 */
		initPageHeight: function () {
			//整体高度
			var winHeight = $(window).height();
			//top高度
			var winTopHeight = $(".shell-top-box").height();
			var leftPartHeight = winHeight-winTopHeight-$("#tabHdLeftTitle").height()- 30 - 30;
			//左边的高度为
			$(".leftHeight").height(leftPartHeight > 600 ? leftPartHeight : 600);
			//设定宽度
			var leftWidth = $("#layout-t").width();
			$(".leftHeight").width(leftWidth);
		},
		initialize : function(){
				//window.sessionStorage.setItem("lockerStatus",0);
				window.sessionStorage.removeItem("lockerStatus");
				//知识维护查询页面跳转
				Path.map("#/moreKmInfoList/:searchKmKey").to(function () {
					common.loadPageDiv("html/shellkmList.html", "shellKmListTable", true);
				});
				common.compatCurrentPage = 1;
				common.productCurrentPage = 1;
		        kmMaintance.bindRoot();
		        kmMaintance.initLeftTree();
		        kmMaintance.bindEvent();
		        kmMaintance.initCategoryList();
		        kmMaintance.initPageHeight();
		}

	};


 

